package cn.xin.system.controller;

import cn.xin.base.util.AjaxResult;
import cn.xin.base.util.PageList;
import cn.xin.system.domain.Dictionarydetail;
import cn.xin.system.query.DictionarydetailQuery;
import cn.xin.system.service.DictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DictionarydetailController {
    @Autowired
    private DictionarydetailService dictionarydetailService;

    @GetMapping
    public List<Dictionarydetail> findAll() {
        return dictionarydetailService.findAll();
    }

    @GetMapping("/{id}")
    public Dictionarydetail findById(@PathVariable("id")Long id) {
        return dictionarydetailService.findById(id);
    }


    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){

        try {
            if (dictionarydetail.getId()==null){
                dictionarydetailService.add(dictionarydetail);
            }else {
                dictionarydetailService.update(dictionarydetail);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        dictionarydetailService.delete(id);
        return AjaxResult.me().success();
    }

    @PostMapping()
    public PageList<Dictionarydetail> queryByCount(@RequestBody DictionarydetailQuery query){
        return  dictionarydetailService.queryByPage(query);
    }

    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        dictionarydetailService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/findByTypeId/{typeId}")
    public List<Dictionarydetail> findByDeptId(@PathVariable("typeId") Long typeId){
        return dictionarydetailService.findByDeptId(typeId);
    }
}
